<template>
  <a-modal v-model:visible="visible" fullscreen @before-ok="submit" unmount-on-close>
    <template #title>维护 {{ currentRow.name }} 列表</template>
    <ma-crud :options="crud" :columns="columns" ref="crudRef"/>
  </a-modal>
</template>

<script setup>
import {ref, reactive, nextTick} from 'vue'
import templateExcelExportDetail from '@/api/template/templateExcelExportDetail'

const crudRef = ref()
const visible = ref(false)
const currentRow = ref({id: undefined, name: undefined})
const form = ref({})

const open = (row) => {
  currentRow.value = row
  visible.value = true
  nextTick(() => {
    crudRef.value.getColumnService().get('path').setAttr('dict', {
          url: 'template/excelObject/dtoFields',
          params: {dto: currentRow.value.export_main_object_name},
          translation: true
        }
    )
    crudRef.value.requestData()
  })
}

const crud = ref({
  autoRequest: false,
  api: templateExcelExportDetail.getList,
  showIndex: false,
  pageLayout: 'fixed',
  rowSelection: {showCheckedAll: true},
  showTools: false,
  add: {show: true, api: templateExcelExportDetail.save, auth: ['template:excelExportDetail:save']},
  edit: {show: true, api: templateExcelExportDetail.update, auth: ['template:excelExportDetail:update']},
  beforeAdd: (form) => {
    form.template_id = currentRow.value?.id
    return true
  },
  beforeRequest: (params) => params.template_id = currentRow.value.id,
  delete: {
    show: true,
    api: templateExcelExportDetail.deletes,
    auth: ['template:excelExportDetail:delete']
  }
})

const columns = reactive([
  {title: '字段', dataIndex: 'path', formType: 'select'},
  {title: '表头', dataIndex: 'title_name',},
  {title: '排序', dataIndex: 'column_no',},
])

defineExpose({open})
</script>
